package com.dlc.shop.user.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.app.dto.CouponDto;
import com.dlc.shop.user.common.dao.UserLevelCouponMapper;
import com.dlc.shop.user.common.dto.LevelCouponNumDTO;
import com.dlc.shop.user.common.model.Coupon;
import com.dlc.shop.user.common.model.UserLevelCoupon;
import com.dlc.shop.user.common.service.UserLevelCouponService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Service
@AllArgsConstructor
public class UserLevelCouponServiceImpl extends ServiceImpl<UserLevelCouponMapper, UserLevelCoupon> implements UserLevelCouponService {

    private final UserLevelCouponMapper userLevelCouponMapper;



    @Override
    public void insertBatchCoupon(Long[] coupons ,Long id) {
        userLevelCouponMapper.insertBatchCoupon(coupons, id);
    }

    @Override
    public void delBatchCoupon(Long[] coupons, Long id) {
        userLevelCouponMapper.delBatchCoupon(coupons, id);
    }

    @Override
    public List<Coupon> getCouponListByLevelId(Long id) {
        return userLevelCouponMapper.getCouponListByLevelId(id);
    }

    @Override
    public List<CouponDto> listCouponAndGetInfoByLevelId(Long levelId, String userId) {
        return userLevelCouponMapper.listCouponAndGetInfoByLevelId(levelId, userId);
    }

    @Override
    public List<Long> listLevelIdByCouponIds(List<Long> couponIds) {
        if (CollectionUtils.isEmpty(couponIds)) {
            return new ArrayList<>();
        }
        return userLevelCouponMapper.listLevelIdByCouponIds(couponIds);
    }

    @Override
    public Map<Long, Integer> countLevelCouponNums(List<Long> levelIds) {
        List<LevelCouponNumDTO> levelCouponNumList = userLevelCouponMapper.countLevelCouponNums(levelIds);
        if (CollectionUtils.isEmpty(levelCouponNumList)) {
            return new HashMap<>(16);
        }
        return levelCouponNumList.stream().collect(Collectors.toMap(LevelCouponNumDTO::getLevelId, LevelCouponNumDTO::getCouponNum));
    }

}
